package cn.maihe.elg.operation.supports.electronicseal.kt.mixca;

import cn.maihe.elg.operation.config.ElgConfig;
import cn.maihe.elg.operation.config.ElgResourceConfig;
import cn.maihe.elg.operation.config.MixCaConfig;
import cn.maihe.elg.operation.model.enums.AttachmentFileType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeAttachmentInfoDO;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.GuaranteeAttachmentInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.nio.file.Path;

/**
 * @Description
 * @Author WJH
 * @Date 2021/11/09
 */
@Service
@Slf4j
public class MixCaSignService {

    @Resource
    private ElgResourceConfig elgResourceConfig;
    @Resource
    private ElgResourceService elgResourceService;

    @Resource
    private MixCaConfig mixCaConfig;
    @Resource
    private GuaranteeAttachmentInfoService guaranteeAttachmentInfoService;
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private ElgConfig elgConfig;

    /**
     * 未签署的保函申请书构造融合签章客户端所需的json参数并base64编码返回
     */
    public String buildMixCaBase64(Path applyFile, String acceptOrderNo) {
        String relativeApplyPath = elgResourceService.getRelativePath(applyFile);
        String url = elgConfig.getApiBaseUrl() + "/filePath" + relativeApplyPath;
        String uploadUrl = mixCaConfig.getUploadUrl();
        String base64Str = ToMixCaParamsDto.buildBase64(url, uploadUrl, acceptOrderNo);
        return base64Str;
    }

    /**
     * 保存到附件表
     *
     * @param acceptOrderNo
     * @param signedPdfPath
     * @param signedEdcPath
     */
    public void saveSignedApplyFile(String acceptOrderNo, Path signedPdfPath, Path signedEdcPath) {
        AcceptOrderInfoDO orderInfo = acceptOrderInfoService.getByAcceptOrderNo(acceptOrderNo);
        Assert.notNull(orderInfo, "查无此受理订单号");

        String fileType = elgResourceService.getFileType(signedPdfPath);
        String fileName = elgResourceService.getFileName(signedPdfPath);
        String fileMd5 = elgResourceService.getFileMD5AsHex(signedPdfPath);
        String filePath = elgResourceService.getRelativePath(signedPdfPath);

        GuaranteeAttachmentInfoDO signedApplyPdf = GuaranteeAttachmentInfoDO.builder()
                .centerId(orderInfo.getCenterId()).orgId(orderInfo.getOrgId())
                .type(AttachmentFileType.TYPE_11.getType()).acceptOrderNo(acceptOrderNo)
                .fileType(fileType).fileName(fileName).fileMd5(fileMd5).filePath(filePath).remark("已签署申请书pdf")
                .build();

        guaranteeAttachmentInfoService.saveOrUpdateAttachmentInfo(signedApplyPdf);

        if (signedEdcPath != null) {
            String edcFileType = elgResourceService.getFileType(signedEdcPath);
            String edcFileName = elgResourceService.getFileName(signedEdcPath);
            String edcFileMd5 = elgResourceService.getFileMD5AsHex(signedEdcPath);
            String edcFilePath = elgResourceService.getRelativePath(signedEdcPath);

            GuaranteeAttachmentInfoDO signedApplyEdc = GuaranteeAttachmentInfoDO.builder()
                    .centerId(orderInfo.getCenterId()).orgId(orderInfo.getOrgId())
                    .type(AttachmentFileType.TYPE_15.getType()).acceptOrderNo(acceptOrderNo)
                    .fileType(edcFileType).fileName(edcFileName).fileMd5(edcFileMd5).filePath(edcFilePath).remark("已签署申请书edc")
                    .build();

            guaranteeAttachmentInfoService.saveOrUpdateAttachmentInfo(signedApplyEdc);
        }
    }

    /**
     * 未签署的保函申请书构造融合签章客户端所需的json参数并base64编码返回
     */
    public String buildMixCaDecryptBase64(String digitalEnvelope, String acceptOrderNo) {
//        String relativeApplyPath = elgResourceService.getRelativePath(applyFile);
//        String url = elgConfig.getApiBaseUrl() + "/filePath" + relativeApplyPath;
        String envelopeUploadUrl = mixCaConfig.getEnvelopeUploadUrl();
        String base64Str = ToMixCaParamsDto.buildBase64(digitalEnvelope, envelopeUploadUrl, acceptOrderNo);
        return base64Str;
    }
}
